<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 18/4/4
 * Time: 15:49
 */

class BackdropModel
{
    public function listsAction($user)
    {
        $data['lists'] = Db::name('backdrop')
            ->field('id as bid,image,status')
            ->where(['fid'=>$user['fid']])
            ->select();
        return $data;
    }

    public function addAction($content,$user)
    {
        $data['udid']=$user['id'];
        $data['fid']=$user['fid'];
        $data['status'] = $content['status'];
        if(!empty($content['path']) && !empty($content['file'])){
            $model = new ModelModel;
            $data['image'] = $model->moveImg($content['path'], $content['file'], 5);
        }
        $bid = Db::name('backdrop')->insertGetId($data);
        $msg['message'] = '图片上传成功';
        $msg['bid'] = $bid;
        $msg['image'] = $data['image'];
        $msg['status'] = $content['status'];
        return $msg;
    }

    public function editAction($content)
    {
        if(!empty($content['path']) && !empty($content['file'])){
            $image = Db::name('backdrop')->field('bid,image,status')->where(['id'=>$content['bid']])->find();
            if($image['status'] == 1){
                $msg['code'] = 400;
                $msg['message'] = '不可修改系统图片';
            }else{
                $imgUrl = parse_url($image['image']);
                $model = new ModelModel;
                $model->deleteImg($imgUrl['path']);
                $data['image'] = $model->moveImg($content['path'], $content['file'], 5);
                $msg['bid'] = $image['bid'];
                $msg['image'] = $data['image'];
                $msg['status'] = $content['status'];
            }
            $data['status'] = $content['status'];
            Db::name('backdrop')->where(['id'=>$content['bid']])->update($data);
            $msg['message'] = '图片修改成功';
        }else{
            $msg['code'] = 400;
            $msg['message'] = '请上传图片';
        }
        return $msg;
    }
}